package com.aos.eap.activity;

import android.os.SystemClock;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.Constant;
import com.aos.eap.utils.MyUtils;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import butterknife.BindView;
import butterknife.OnClick;

public class SettingActivity extends FreeUI_BaseActivity {

    @BindView(R.id.tvIP)
    TextView tvIP;
    @BindView(R.id.rlIP)
    RelativeLayout rlIP;
    @BindView(R.id.tvVersion)
    TextView tvVersion;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvFileIP)
    TextView tvFileIP;
    @BindView(R.id.ivIp)
    ImageView ivIp;
    @BindView(R.id.ivTimeOut)
    ImageView ivTimeOut;
    @BindView(R.id.tvTimeOut)
    TextView tvTimeOut;
    @BindView(R.id.rlTimeOut)
    RelativeLayout rlTimeOut;
    private int COUNTS = 5;// 点击次数
    private long[] mHits = new long[COUNTS];//记录点击次数
    private long DURATION = 2000;//有效时间

    private FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        freeUI_titleView.setTitle("设置");
    }

    @Override
    protected void initData() {
        tvVersion.setText(FreeApi_Utils.getVerName());
        tvIP.setText(Constant.REQUEST_URL);
        tvFileIP.setText(Constant.FILE_URL);
        tvTimeOut.setText(Constant.DEFAULT_TIME_OUT+"s");
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    @OnClick({R.id.rlIP, R.id.rlFileIP, R.id.rlTimeOut,R.id.llCaiDan})
    public void onClick(View view) {
        if (MyUtils.isFastClick()){
            showToast("您点击的太快了",false);
            return;
        }
        switch (view.getId()) {
            case R.id.rlIP:
                freeUI_generalFragmentDialog = FreeApi_DialogUtils.getScanDialog(this, "请输入IP地址", Constant.REQUEST_URL, new FreeUI_TextCallback() {
                    @Override
                    public void onSuccess(String s) {
                        freeUI_generalFragmentDialog.dismiss();
                        Constant.REQUEST_URL = s;
                        tvIP.setText(Constant.REQUEST_URL);
                        FreeApi_SharePreferencesUtils.setSharePre("URL_FILE", "URL_IP", s, getApplicationContext());
                        showToast("保存成功", true);
                    }
                });
                freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "URL_IP_DIALOG");
                break;
            case R.id.rlFileIP:
                freeUI_generalFragmentDialog = FreeApi_DialogUtils.getScanDialog(this, "请输入IP地址", Constant.FILE_URL, new FreeUI_TextCallback() {
                    @Override
                    public void onSuccess(String s) {
                        freeUI_generalFragmentDialog.dismiss();
                        Constant.FILE_URL = s;
                        tvFileIP.setText(Constant.FILE_URL);
                        FreeApi_SharePreferencesUtils.setSharePre("URL_FILE", "FILE_IP", s, getApplicationContext());
                        showToast("保存成功", true);
                    }
                });
                freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "FILE_IP_DIALOG");
                break;
            case R.id.rlTimeOut:
                freeUI_generalFragmentDialog = FreeApi_DialogUtils.getScanDialog(this, "请输入超时时间", Constant.DEFAULT_TIME_OUT, new FreeUI_TextCallback() {
                    @Override
                    public void onSuccess(String s) {
                        freeUI_generalFragmentDialog.dismiss();
                        Constant.DEFAULT_TIME_OUT = s;
                        tvTimeOut.setText(Constant.DEFAULT_TIME_OUT+"s");
                        FreeApi_SharePreferencesUtils.setSharePre("TIME_OUT_FILE", "TIME_OUT2", s, getApplicationContext());
                        showToast("保存成功，请重启APP生效", true);
                        rlTimeOut.setVisibility(View.GONE);
                    }
                });
                freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "TIME_OUT_DIALOG");
                break;
            case R.id.llCaiDan:
                //将mHints数组内的所有元素左移一个位置
                System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
                //获得当前系统已经启动的时间
                mHits[mHits.length - 1] = SystemClock.uptimeMillis();
                if (mHits[0] >= (SystemClock.uptimeMillis() - DURATION)) {
                    mHits = new long[COUNTS];
                    rlTimeOut.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

}

